/* ----------------------------------------------------------------------------------------------------------------------------------------------- /

ToolWindow - 类名称
修改许可权限 - 修改人名称

< --- Public Vars ------------------------------------------------------------------------------------------------------------------------------ >

< --- Public Functions ------------------------------------------------------------------------------------------------------------------------ >

※ 构造函数
public function ToolWindow()

/ ----------------------------------------------------------------------------------------------------------------------------------------------- */
package editor.ui{
	import com.bit101.components.HSlider;
	import com.bit101.components.InputText;
	import com.bit101.components.Label;
	import com.bit101.components.List;
	import com.bit101.components.PushButton;
	import com.bit101.components.TextArea;
	import com.bit101.components.Window;
	
	import editor.Notification.V2MNotification;
	import editor.common.AppFacade;
	import editor.common.Common;
	import editor.common.CursorManager;
	import editor.manager.MapManger;
	import editor.map.elements.AvatarNpc;
	import editor.map.elements.Npc;
	import editor.map.elements.Teleport;
	
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	
	public class ToolWindow extends Window{
		
		private var _brushSlider:HSlider;
		private var _alphaSlider:HSlider;
		private var _brushLabel:Label;
		private var _brushSizeLabel:Label;
		private var _addNpcBtn:PushButton;
		private var _addPortBtn:PushButton;
		private var _npcList:List;
		
		private var _npcIdLabel:Label;
		private var _npcXLabel:Label;
		private var _npcYLabel:Label;
		private var _npcIdInput:InputText;
		private var _setNpcNameBtn:PushButton;
		
		private var _portInfoLabel:Label;
		private var _selectedNpc:Npc;
		private var _deleteNpcBtn:PushButton;
		
		public function ToolWindow(parent:DisplayObjectContainer=null, xpos:Number = 0, ypos:Number = 0)
		{
			super(parent, xpos, ypos, "NPC 和 笔刷");
			setSize(120, 400);
			_brushSlider = new HSlider(this, 10, 30);
			_brushSlider.width = 80;
			_brushSlider.maximum = 10;
			_brushSlider.minimum = 1;
			_brushSlider.tick = 1;
			_brushSlider.addEventListener(Event.CHANGE, onBrushSliderChange);
			
			_alphaSlider = new HSlider(this, 10, 43);
			_alphaSlider.width = 80;
			_alphaSlider.maximum = 1;
			_alphaSlider.minimum = 0;
			_alphaSlider.tick = 0.05;
			_alphaSlider.value = 1;
			_alphaSlider.addEventListener(Event.CHANGE, onAlphaSliderChange);
			
			_brushLabel 	= new Label(this, 10, 10, "笔刷大小:");
			_brushSizeLabel = new Label(this, 95, 25, "1");
			
			
			
			
			_npcList = new List(this, 10, 55);
			_npcList.setSize(100,210);
			_npcList.addEventListener(Event.SELECT, onSelect);
			
			_npcIdLabel = new Label(this, 10, 285, "ID:");
			_npcXLabel	= new Label(this, 10, 305,"x: ");
			_npcYLabel	= new Label(this, 50, 305,"y: ");
			
			_npcIdInput　= new InputText(this, 30, 285);
			_npcIdInput.setSize(60,20);
			
			_setNpcNameBtn = new PushButton(this, 95, 285, "ok", clickSetNpcid);
			_setNpcNameBtn.setSize(20, 20);
			
			_addNpcBtn　 = new PushButton(this, 10, 325, "增加NPC", onAddNpc);
			_deleteNpcBtn = new PushButton(this, 10, 350, "删除NPC", onDeleteNpc);
			
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
			
		}
		
		private function onEnterFrame(evt:Event):void
		{
			if(_selectedNpc is Npc)
			{
				updateNpcInfo(_selectedNpc.npcId, _selectedNpc.x, _selectedNpc.y);
			}
		}
		
		//alpha大小改变
		private function onAlphaSliderChange(evt:Event):void
		{
			Common.map.tileLayer.lineAlpha = _alphaSlider.value;
		}
		
		//刷子大小改变
		private function onBrushSliderChange(evt:Event):void
		{
			_brushSizeLabel.text = _brushSlider.value.toString();
			MapManger.getInstance().tileLayer.brushSize = _brushSlider.value;
		}
		
		//增加npc
		private function onAddNpc(evt:Event):void
		{
			Common.cursor.state = CursorManager.STATE_ADD_NPC;
		}
		
		//删除npc
		private function onDeleteNpc(evt:Event):void
		{
			if(_selectedNpc != null)
			{
				Common.map.npcLayer.deleteNpc(_selectedNpc);
				//将选择的npc清除
				clearSelectedNpc();
			}
		}
		
		//增加传送点
		private function onAddPort(evt:Event):void
		{
			Common.cursor.state = CursorManager.STATE_ADD_PORT;
		}
		
		//选中一个npc
		private function onSelect(evt:Event):void
		{
			if(_npcList.selectedItem != null)
			{
				setSelectedNpc(Common.map.npcLayer.getNpcByIndex(_npcList.selectedItem.index));
				AppFacade.getIntance().sendNotification(V2MNotification.SET_MAP_VIEW,
					{
						vx:_selectedNpc.x,
						vy:_selectedNpc.y
					});
			}
		}
		
		//设置npc id
		private function clickSetNpcid(evt:Event):void
		{
			if(_selectedNpc != null)
			{
				var id:String = _npcIdInput.text
				id = id.replace(/^\s*/g,"");
				id = id.replace(/\s*$/g,"");
				
				_selectedNpc.npcId = id;
				_npcIdInput.text = _selectedNpc.npcId;
				updateNpcList();
			}
			
		}
		
		//清除选中的npc
		public function clearSelectedNpc():void
		{
			if(_selectedNpc != null)
			{
				_selectedNpc.isSelected = false;
				_selectedNpc = null;
				_npcIdInput.text = "";
			}
		}
		
		
		/**
		 * 设置被选中的npc
		 * @param idx 
		 */		
		public function setSelectedNpc(npc:Npc):void
		{

			if(_selectedNpc != null) _selectedNpc.isSelected = false;
			_selectedNpc = npc;
			_npcIdInput.text = _selectedNpc.npcId;
			_selectedNpc.isSelected = true;
		
		}
		
		/**
		 * 更新npc列表 
		 */		
		public function updateNpcList():void
		{
			var list:Vector.<AvatarNpc> = Common.map.npcLayer.npcList;
			
			try{
				this.content.removeChild(_npcList);
			}catch(e:Error){}
			clearSelectedNpc();
			_npcList.removeEventListener(Event.SELECT, onSelect);
			_npcList = new List(this, 10, 55);
			_npcList.setSize(100,210);
			_npcList.addEventListener(Event.SELECT, onSelect);
			if(list.length != 0)
			{
				for(var i:int = 0; i < list.length ; i ++)
				{
					var npc:Npc = list[i];
					if(npc is Teleport)
						_npcList.addItem({label:"传送点 : " + npc.npcId, index:i});
					else 
						_npcList.addItem({label:"NPC  : " + npc.npcId, index:i});
				}
			}
			
		}
		
		/**
		 * 更新npc信息
		 * @param npcId
		 * @param npcX
		 * @param npcY 
		 */		
		public function updateNpcInfo(npcId:String, npcX:int, npcY:int):void
		{
			_npcXLabel.text 	= "x: " + npcX;
			_npcYLabel.text		= "y: " + npcY;
			_npcIdInput.text;
			_npcIdInput.text;
		}
		//
	}
}

